package com.youximao.sdk.app.pay.service;

import android.content.Context;

import com.youximao.sdk.lib.common.network.NetworkRequestsUtil;
import com.youximao.sdk.lib.common.sdk.GameCatSDKListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by davy on 16/6/18.
 */
public class GameCatPayApi {

    /**
     * 请求代金券
     */
    public static void queryAvailableCoupon(String price, String goodsType, final GameCatSDKListener listener, final Context context) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("price", price);
        paramsMap.put("goodsType", goodsType);
        Map<String, String> headersMap = new HashMap<>();

        String queryAvailableCouponUrl = GameCatPayUrl.initEvnUrl() + GameCatPayUrl.QUERY_AVAILABLE_COUPON_URL;
        NetworkRequestsUtil.getInstance().startNetworkRequests(queryAvailableCouponUrl, headersMap, paramsMap,
                new GameCatSDKListener() {
                    @Override
                    public void onSuccess(JSONObject message) {
                        listener.onSuccess(message);
                    }

                    @Override
                    public void onFail(String message) {
                        listener.onFail(message);
                    }
                }, context);
    }

    /**
     * 创建订单
     */
    public static void createOrder(Map<String, String> params, final GameCatSDKListener listener, final Context context) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.putAll(params);
        Map<String, String> headersMap = new HashMap<>();

        String createOrderUrl = GameCatPayUrl.initSDKPayUrl() + GameCatPayUrl.CREATE_ORDER_URL;
        NetworkRequestsUtil.getInstance().startNetworkRequests(createOrderUrl, headersMap, paramsMap,
                new GameCatSDKListener() {
                    @Override
                    public void onSuccess(JSONObject message) {
                        listener.onSuccess(message);
                    }

                    @Override
                    public void onFail(String message) {
                        listener.onFail(message);
                    }
                }, context);

    }

    /**
     * 请求支付方式
     *
     * @Param platform(7 sdk-安卓购买道具 8-sdk-ios喵点充值 9-sdk-安卓喵点充值 10-sdk-ios购买游戏道具)
     */
    public static void queryPayWay(String price, String platform, final GameCatSDKListener listener, final Context context) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("platform", platform);
        paramsMap.put("price", price);
        Map<String, String> headersMap = new HashMap<>();

        String queryPayWayUrl = GameCatPayUrl.initEvnUrl() + GameCatPayUrl.QUERY_PAY_WAY_URL;
        NetworkRequestsUtil.getInstance().startAsynchronousRequests(queryPayWayUrl, headersMap, paramsMap,
                new GameCatSDKListener() {
                    @Override
                    public void onSuccess(JSONObject message) {
                        listener.onSuccess(message);
                    }

                    @Override
                    public void onFail(String message) {
                        listener.onFail(message);
                    }
                }, context);
    }

    /**
     * 发起支付
     */
    public static void sdkPay(String interfaceId, String orderId, final GameCatSDKListener listener, final Context context) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("interfaceId", interfaceId);
        paramsMap.put("orderId", orderId);
        Map<String, String> headersMap = new HashMap<>();

        String sdkPayUrl = GameCatPayUrl.initEvnUrl() + GameCatPayUrl.SDK_PAY_URL;
        NetworkRequestsUtil.getInstance().startNetworkRequests(sdkPayUrl, headersMap, paramsMap,
                new GameCatSDKListener() {
                    @Override
                    public void onSuccess(JSONObject message) {
                        listener.onSuccess(message);
                    }

                    @Override
                    public void onFail(String message) {

                        listener.onFail(message);
                    }
                }, context);

    }


}
